package com.neuedu.controller;

import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Doctor;
import com.neuedu.entity.Register;
import com.neuedu.service.RegisterService;
import com.neuedu.vo.RegisterCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Api("挂号模块")
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Resource
    private RegisterService registerService;

    @ApiOperation("添加挂号")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Register register) {

        System.out.println("接收数据后=" + register);

        //接收数据后=Register(registerId=null, patientName=xxx, patientGender=女, registerDate=Fri Oct 13 00:00:00 CST 2023, registerNoon=1, settleMode=1, feeMode=1, deptId=1, levelId=1, doctorId=22, registerAmount=11, registerTime=null, status=null, speak=null, result=null, diseaseId=null)

        if (registerService.add(register)) {
            return ServerResponse.success("挂号成功，病历号为【" + register.getRegisterId() + "】");
        } else {
            return ServerResponse.error("挂号失败");
        }

    }

    @ApiOperation("查询挂号列表")
    @GetMapping("/list")
    public ServerResponse list(RegisterCondition registerCondition, HttpSession session) {

        System.out.println(registerCondition);

        //从session属性范围中获取当前登录的用户对象
        Doctor doctor = (Doctor) session.getAttribute("currentUser");


        System.out.println(doctor.getDoctorId());
        //填充医生编号
        registerCondition.setDoctorId(doctor.getDoctorId());

        List<Register> registerList = registerService.list(registerCondition);

        return ServerResponse.success("").data("registerList", registerList);
    }

    @ApiOperation("查询挂号人数统计信息")
    @GetMapping("/count")
    public ServerResponse count(HttpSession session) {

        //从session属性范围中获取当前登录的用户对象
        Doctor doctor = (Doctor) session.getAttribute("currentUser");

        //调用业务方法
        Map<String, Integer> map = registerService.getRegisterCount(doctor.getDoctorId());

        return ServerResponse.success("")
                .data("newCount", map.get("newCount"))
                .data("receiveCount", map.get("receiveCount"))
                .data("finishCount", map.get("finishCount"))
                .data("totalCount", map.get("totalCount"));
    }

    @ApiOperation("更新病历")
    @PutMapping("/modify")
    public ServerResponse modify(String registerId, String speak) {

       //调用业务方法
       if(registerService.modifyRegister(registerId, speak)){
           return ServerResponse.success("更新病历成功");
       }else{
           return ServerResponse.success("更新病历失败");
       }
    }

    @ApiOperation("确诊病历")
    @PutMapping("/insure")
    public ServerResponse insure(String registerId, String result, int diseaseId) {

        //调用业务方法
        if(registerService.insureRegister(registerId, result, diseaseId)){
            return ServerResponse.success("确诊病历成功");
        }else{
            return ServerResponse.success("确诊病历失败");
        }
    }
}